<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title></title>
  <link rel="stylesheet" href="/css/main.css"/>
  <link rel="stylesheet" href="/cms/layui/css/layui.css">
  <script src="/js/uikit.js"></script>
</head>

<body>

<div th:insert="~{common::#header}"></div>

<div class="uk-section uk-section-muted">
  <div class="uk-container">
    <div class="uk-background-default uk-border-rounded uk-box-shadow-small">
      <div class="uk-container uk-container-xsmall uk-padding-large">
        <article class="uk-article">
          <h1 class="uk-article-title" style="text-align: center;">管理我的资源</h1>
          <div class="uk-article-content">
            <form class="uk-form-stacked uk-margin-medium-top layui-form" onsubmit="return false;">


              <div class="uk-margin-bottom">
                <label class="uk-form-label" for="tag">上传资源(选择之后自动上传)</label>
                <div class="uk-form-controls">
                  <input id="tag" class="uk-input uk-border-rounded" placeholder="上传资源" autocomplete="off" type="file">

                  <input type="hidden" id="busLicense">
                </div>
              </div>


              <div class="uk-margin-bottom">
                <label class="uk-form-label" for="tag">资源标题</label>
                <div class="uk-form-controls">
                  <input id="title" class="uk-input uk-border-rounded" placeholder="资源标题" autocomplete="off"
                         type="text">
                </div>
              </div>




              <div class="uk-margin-bottom">
                <label class="uk-form-label" for="tag">资源描述（键入Enter按键进行添加）</label>
                <div class="uk-form-controls">
                  <input id="description" class="uk-input uk-border-rounded" placeholder="资源描述（键入Enter按键进行添加）" autocomplete="off"
                         type="text">
                </div>
              </div>

            </form>
          </div>
        </article>
      </div>
    </div>
  </div>
</div>
<div class="uk-section uk-section-muted">
  <div class="uk-container">
    <div class="uk-margin-medium-top">

      <div th:each="download:${downloads}"
           class="uk-card  uk-card-default uk-card-hover uk-card-body uk-inline uk-border-rounded uk-width-1-1"
           style="border-bottom: 1px dotted #583ABC; border-radius: 0px;">
        <h3 class="uk-card-title uk-margin-remove uk-text-primary" th:text="${download.title}"></h3>
        <p class="uk-margin-small-top" th:text="${download.description}"></p>
        <div class="uk-article-meta uk-flex uk-flex-middle">

          <img class="uk-border-circle uk-avatar-small" style="height: 36px;width: 36px;"
               th:src="'/' + ${download.headImg}" alt="">
          <div style="margin-top: 10px">
            本资源上传于：
            <time class="uk-margin-small-right"
                  th:text="${#dates.format(download.createTime, 'yyyy-MM-dd hh:mm:ss')}"></time>
            <br>
            更新于：
            <time th:text="${#dates.format(download.updateTime, 'yyyy-MM-dd hh:mm:ss')}"></time>

          </div>

          <div class="uk-text-center" style="margin-left: 66%;">
            <input class="uk-button uk-button-primary uk-border-rounded" type="submit" th:onclick="deleteTag([[${download.id}]])" value="删除">
          </div>

        </div>
      </div>

    </div>
    <div class="uk-text-center uk-margin-medium-top">

    </div>
  </div>
</div>

<script src="/js/awesomplete.js"></script>
<script src="/js/custom.js"></script>
<script src="/js/jquery.min.js"></script>
<script src="/cms/layui/layui.js" charset="utf-8"></script>

<script>
  layui.use(['layedit', 'form'], function () {
    var form = layui.form;
    var $ = layui.$;

    // 上传图片
    $(document).on('change', '#tag', function () {

      var editImage = $("#tag").get(0).files[0];
      var formDate = new FormData();
      formDate.append("editImage", editImage);
      $.ajax({
        url: '/file/upload',
        type: 'POST',
        processData: false,
        contentType: false,
        async: false,
        data: formDate,
        success: function (result) {
          if (result.code == 200) {
            $("#busLicense").val("");
            $("#busLicense").val(result.data);
            layer.msg(result.msg)
          } else {
            $("#busLicense").val("");
            layer.msg(result.msg)
          }
        }
      })
    })

  });
  document.onkeydown = function (e) {
    if (e.keyCode == 13) {
      var description = $("#description").val();
      if (!description) {
        layer.msg("请输入资源描述！");
        return false;
      }
      $.ajax({
        url: '/my/download/upload',
        type: 'POST',
        dataType: 'JSON',
        data: {
          path:$("#busLicense").val(),
          description:description,
          title:$("#title").val()
        },
        success: function (result) {
          if (result.code == 200) {
            alert(result.msg)
            window.location.reload();
          } else {
            alert(result.msg)
            return false;
          }
        }
      })

    }
  }


  function deleteTag(index) {
    if (!confirm("确定删除？")) {
      return false;
    }
    $.ajax({
      url:'/my/download/deleteById?downloadId=' + index,
      success:function (result) {
        if(result.code == 200) {
          window.location.reload();
        } else {
          alert(result.msg);
          return false;
        }
      }
    })


  }
</script>

</body>

</html>